package Editeur;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import Impl.Terrain;
import Impl.Type;

public class EditeurGraph extends JPanel implements ActionListener, ChangeListener{
	private static final long serialVersionUID = 6289845703131252832L;
	private int size_x=5, size_y=5;
	private Type typeChoice = Type.Hero;
	private String[] types = {"Hero", "Diamant", "Rocher", "Mur", "Terre", "Vide", "Sortie"};
	
	private BorderLayout myLayout = new BorderLayout();
	private JPanel menuGauche = new JPanel();
	private TerrainPanel afficheurTerrain;
	private GridLayout LayoutGauche = new GridLayout(10,1);
	
	// JLabels
	private JLabel titrePage = new JLabel("Creez votre nouveau niveau Boulder Dash", JLabel.CENTER);
	private JLabel basDePage = new JLabel("Vous devez avoir un hero, une porte et au moins un diamant...", JLabel.CENTER);
	private JLabel tailleX = new JLabel("size X : ", JLabel.LEFT);
	private JLabel tailleY = new JLabel("size Y : ", JLabel.LEFT);
	private JLabel selectionString = new JLabel("Quel type ajouter ?", JLabel.LEFT);
	
	// JComboBox
	private JComboBox selectType;
	
	// JSliders
	private JSlider sizeX = new JSlider(JSlider.HORIZONTAL, 2, 20, 5);
	private JSlider sizeY = new JSlider(JSlider.HORIZONTAL, 2, 20, 5);
	
	// JButton
	private JButton boutonSave = new JButton("sauver");

	
	
	public EditeurGraph(){
		setLayout(myLayout);
		
		// Titre de la Page
		add(titrePage, BorderLayout.PAGE_START);
		
		// Menu Gauche 
		add(menuGauche, BorderLayout.LINE_START);
		selectType = new JComboBox(types);
		selectType.setSelectedIndex(0);
		selectType.addActionListener(this);
		selectType.setMaximumSize(new Dimension(10, 10));
		//selectType.setPreferredSize(new Dimension(100, 50));
		menuGauche.setLayout(LayoutGauche);
		menuGauche.add(selectionString);
		menuGauche.add(selectType);
		menuGauche.add(tailleX);
		sizeX.addChangeListener(this);
		sizeX.setMajorTickSpacing(2);
		sizeX.setMinorTickSpacing(1);
		sizeX.setPaintTicks(true);
		sizeX.setPaintLabels(true);
		menuGauche.add(sizeX);
		menuGauche.add(tailleY);
		sizeY.addChangeListener(this);
		sizeY.setMajorTickSpacing(2);
		sizeY.setMinorTickSpacing(1);
		sizeY.setPaintTicks(true);
		sizeY.setPaintLabels(true);
		menuGauche.add(sizeY);
		boutonSave.setEnabled(false);
		boutonSave.addActionListener(this);
		menuGauche.add(boutonSave);
		
		// Centre
		Terrain terrain = new Terrain();
		terrain.init(4, 5);
		//terrain.addDiamant(2, 1);
		afficheurTerrain = new TerrainPanel(4, 5, terrain, this);
		add(afficheurTerrain, BorderLayout.CENTER);
		
		// A Droite
		//add(new JLabel("ok"), BorderLayout.LINE_END);
		
		// Fin de page
		add(basDePage, BorderLayout.PAGE_END);
		
	}
	

	@Override
	public void actionPerformed(ActionEvent e) {
		String myChoice;
		if(e.getSource()==selectType){			// si on choisi le placement d'un objet
			myChoice = (String) selectType.getSelectedItem();
			if(myChoice.equals("Hero"))
				typeChoice = Type.Hero;
			else if(myChoice.equals("Diamant"))
				typeChoice = Type.Diamant;
			else if(myChoice.equals("Rocher"))
				typeChoice = Type.Rocher;
			else if(myChoice.equals("Mur"))
				typeChoice = Type.Mur;
			else if(myChoice.equals("Terre"))
				typeChoice = Type.Terre;
			else if(myChoice.equals("Vide"))
				typeChoice = Type.Vide;
			else if(myChoice.equals("Sortie"))
				typeChoice = Type.SortieFermee;
		}
		else if(e.getSource()==boutonSave){
			afficheurTerrain.save("niveau1.txt");
		}
	}

	@Override		// Gestion du Slider
	public void stateChanged(ChangeEvent e) {
		if(e.getSource() == sizeX){
			if(sizeX.getValueIsAdjusting())
				return;
			
			size_x = sizeX.getValue();
			afficheurTerrain.modify(size_x, size_y);
		}
		else if(e.getSource() == sizeY){
			if(sizeY.getValueIsAdjusting())
				return;
			
			size_y = sizeY.getValue();
			afficheurTerrain.modify(size_x, size_y);
		}
	}
	
	public Type getChoice(){
		return typeChoice;
	}
	
	public void MAJ(int nbHero, int nbPorte, int nbDiamant){
		System.out.println("hero:"+nbHero+" portes:"+nbPorte+" diamants:"+nbDiamant);
		if(nbHero==1 && nbDiamant>0 && nbPorte==1){
			boutonSave.setEnabled(true);
			basDePage.setText("Bravo ! Vous pouvez sauvegarder votre niveau");
		}
		else{
			boutonSave.setEnabled(false);
			basDePage.setText("Vous devez avoir un hero, une porte et au moins un diamant...");
		}
	}

}
